package org.kev.transit.worker;

import java.util.ArrayList;
import org.kev.transit.data.TimeTable;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

public class TimeTableUpdateWorker implements Runnable{
	private static final String TAG = "TimeTableUpdateWorker";
	private Activity mParent = null;
	private ArrayList <TimeTable> mList = null;


	public TimeTableUpdateWorker(Activity parent, ArrayList <TimeTable> list){
		mParent=parent;
		mList=list;
	}
	/**
	 * 
	 */
	public void run() {
		int count = mList.size();
		String where ="";
		Cursor cursor = null;
		Uri curi=TimeTable.CONTENT_URI;
		for(int i=0;i<count;i++){
			TimeTable data = mList.get(i);
			//uri.addId(Long.parseLong(s.id) );
			where = TimeTable.ROUTE_TYPE + "=? AND "+  
					TimeTable.ROUTE + "=? AND " +
					TimeTable.DIRECTION + "= ? AND "+
					TimeTable.STOP_ID+ "= ? AND "+
					TimeTable.TIME_ID+"= ?  ";
			Log.d(TAG,"wheres="+where);
			if(data.routeType==null || data.route==null || data.direction==null ||
					data.stopId==null || data.id ==null){
				Log.e(TAG," NULL PARMS EXIT="+data.routeType+data.route+
						data.direction+data.stopId+data.id	);	
				return;
			} //passing a null here can crash the VM

			String args[]={data.routeType,data.route,data.direction,data.stopId,data.id};

			try{
				cursor = mParent.managedQuery(curi, new String[] 
				     {TimeTable.TIME_ID},where,args,null);
				//update lookup table
				Log.d(TAG,"found in db #"+cursor.count());
				
				if(cursor.count()==0){
					insertTime(data);
				}else{
					updateTime(data);
				}
			}catch(Exception e){
				Log.e(TAG,"UNKNOWN error will be ignored",e);
			}finally{
				if(cursor!=null)
					cursor.close();
			}
		}

	}

		/**
		 * 
		 * @param id
		 * @param title
		 * @param lat
		 * @param lon
		 * @return
		 */
		private Uri insertTime(TimeTable t){
			if (mParent ==null){
				return null;
			}
			Log.d(TAG, "+++insertTimeTable");
			ContentValues values = new ContentValues();
			//values.put(Stop._ID, id);
			values.put(TimeTable.STOP_ID,t.id);
			values.put(TimeTable.TIME, t.time);
			values.put(TimeTable.ROUTE_TYPE,t.routeType);
			values.put(TimeTable.ROUTE, t.route);
			values.put(TimeTable.DIRECTION,t.direction);
			values.put(TimeTable.STOP_ID,t.stopId);	
			values.put(TimeTable.MODIFIED,new  Long(System.currentTimeMillis() ) );
			
			Uri curl = mParent.getContentResolver().insert(TimeTable.CONTENT_URI,values);
			return curl;
		}
		private int updateTime(TimeTable data){
			if (mParent ==null){
				return 0;
			}
			Log.d(TAG, "+++updateTimeTable");
			ContentValues values = new ContentValues();
			//values.put(Stop._ID, id);
			values.put(TimeTable.TIME_ID,data.id);
			values.put(TimeTable.ROUTE_TYPE,data.routeType);
			values.put(TimeTable.ROUTE, data.route);
			values.put(TimeTable.DIRECTION,data.direction);
			values.put(TimeTable.STOP_ID,data.stopId);	
			values.put(TimeTable.TIME, data.time);
			values.put(TimeTable.MODIFIED,new  Long(System.currentTimeMillis() ) );
			String where = 	TimeTable.TIME_ID+"=? AND "+TimeTable.ROUTE_TYPE+"=? AND "+
							TimeTable.ROUTE+"=? AND "+TimeTable.DIRECTION+"=? AND "+
							TimeTable.STOP_ID+"=? ";
			String[] args={data.id,data.routeType,data.route,data.direction,data.stopId};
			int rows = mParent.getContentResolver().update(TimeTable.CONTENT_URI,values,where,args);
			Log.d(TAG,"updated rows="+rows);
			return rows;
		}

}
